# -*- coding:utf-8 -*-

from typing import List
from sqlalchemy.ext.asyncio import AsyncSession
from utils.common_util import CamelCaseUtil, export_list2excel
from module_admin.entity.vo.sys_table_vo import SysTablePageModel
from module_admin.service.sys_table_service import SysTableService
from utils.page_util import PageResponseModel
from module_product.dao.bg_product_dao import BgProductDao
from module_product.entity.do.bg_product_do import BgProduct
from module_product.entity.vo.bg_product_vo import BgProductPageModel, BgProductModel


class BgProductService:
    """
    用户管理模块服务层
    """

    @classmethod
    async def get_bg_product_list(cls, query_db: AsyncSession, query_object: BgProductPageModel, data_scope_sql: str|None) -> [list | PageResponseModel]:
        bg_product_list = await BgProductDao.get_bg_product_list(query_db, query_object, data_scope_sql, is_page=True)
        return bg_product_list

    @classmethod
    async def get_bg_product_by_id(cls, query_db: AsyncSession, bg_product_id: int) -> BgProductModel:
        bg_product = await  BgProductDao.get_by_id(query_db, bg_product_id)
        bg_product_model = BgProductModel(**CamelCaseUtil.transform_result(bg_product))
        return bg_product_model


    @classmethod
    async def add_bg_product(cls, query_db: AsyncSession, query_object: BgProductModel) -> BgProductModel:
        bg_product_model = await BgProductDao.add_bg_product(query_db, query_object)
        return bg_product_model


    @classmethod
    async def update_bg_product(cls, query_db: AsyncSession, query_object: BgProductModel) -> BgProductModel:
        bg_product = await BgProductDao.edit_bg_product(query_db, query_object)
        bg_product_model = BgProductModel(**CamelCaseUtil.transform_result(bg_product))
        return bg_product_model


    @classmethod
    async def del_bg_product(cls, query_db: AsyncSession, bg_product_ids: List[str]):
        await BgProductDao.del_bg_product(query_db, bg_product_ids)


    @classmethod
    async def export_bg_product_list(cls, query_db: AsyncSession, query_object: BgProductPageModel, data_scope_sql) -> bytes:
        bg_product_list = await BgProductDao.get_bg_product_list(query_db, query_object, data_scope_sql, is_page=False)
        filed_list = await SysTableService.get_sys_table_list(query_db, SysTablePageModel(tableName='bg_product'), is_page=False)
        filtered_filed = sorted(filter(lambda x: x["show"] == '1', filed_list), key=lambda x: x["sequence"])
        new_data = []
        for item in bg_product_list:
            mapping_dict = {}
            for fild in filtered_filed:
                if fild["prop"] in item:
                    mapping_dict[fild["label"]] = item[fild["prop"]]
            new_data.append(mapping_dict)
        binary_data = export_list2excel(new_data)
        return binary_data

    @classmethod
    async def get_popular_products(cls, query_db: AsyncSession, limit: int = 3) -> List[dict]:
        """
        获取热门套餐
        """
        popular_products = await BgProductDao.get_popular_products(query_db, limit)
        return popular_products